<template>
    <div class="chat-container">
    <!-- 聊天标题栏 -->
    <div class="chat-header">
      <div class="header-content">
        <div class="ai-info">
          <div class="ai-avatar">
            <span class="ai-icon">🤖</span>
          </div>
          <div class="ai-details">
            <h3 class="ai-name">豆酱AI</h3>
            <p class="ai-status">{{ isLoading ? '正在思考中...' : '在线' }}</p>
          </div>
        </div>
        <div class="chat-actions">
          <button @click="clearChat" class="action-btn" title="清空对话">
            <i class="icon">🗑️</i>
          </button>
        </div>
      </div>
    </div>

    <!-- 聊天消息区域 -->
      <div class="chat-messages" ref="messagesContainer">
        <div v-for="(message, index) in messages" :key="index" class="message-item">
          <div :class="['message', message.type]">
            <div class="message-avatar">
            <div v-if="message.type === 'user'" class="avatar-container">
                <img 
                v-if="getUserAvatarUrl()" 
                :src="getUserAvatarUrl()" 
                :alt="authStore.user?.username || '用户'"
                  @error="handleAvatarError"
                  class="user-avatar-img"
                />
              <div v-else class="default-avatar-container" :style="{ background: getAvatarBackgroundColor() }">
                <span class="default-avatar-text">{{ getAvatarText() }}</span>
              </div>
            </div>
            <span v-else class="ai-message-avatar">🤖</span>
            </div>
            <div class="message-content">
            <div class="message-bubble">
              <div 
                class="message-text" 
                v-html="formatMessage(message.content)"
              ></div>
              <div class="message-footer">
              <div class="message-time">{{ formatTime(message.timestamp) }}</div>
                <div v-if="message.type === 'ai'" class="message-actions">
                  <button @click="copyMessage(message.content)" class="action-btn" title="复制">
                    <i class="icon">📋</i>
                  </button>
                  <button @click="likeMessage(index)" class="action-btn" :class="{liked: message.liked}" title="点赞">
                    <i class="icon">👍</i>
                  </button>
                </div>
              </div>
            </div>
            </div>
          </div>
        </div>
        
      <!-- 加载状态 - 只在非流式输出时显示 -->
      <div v-if="isLoading && !isStreaming" class="message-item">
          <div class="message ai">
            <div class="message-avatar">
            <span class="ai-message-avatar">🤖</span>
            </div>
            <div class="message-content">
            <div class="message-bubble">
              <div class="typing-indicator">
                <div class="typing-dots">
                <span></span>
                <span></span>
                <span></span>
                </div>
                <span class="typing-text">AI正在思考...</span>
              </div>
              </div>
            </div>
          </div>
        </div>
      </div>

    <!-- 输入区域 -->
      <div class="chat-input-container">
      <!-- 快速操作按钮 -->
      <div class="quick-actions" v-if="!isInputFocused">
          <button 
            v-for="action in quickActions" 
            :key="action.id"
            @click="sendQuickMessage(action.message)"
            class="quick-action-btn"
            :disabled="isLoading"
          >
          <span class="action-icon">{{ action.icon }}</span>
          <span class="action-label">{{ action.label }}</span>
          </button>
        </div>
        
      <!-- 输入框区域 -->
        <div class="input-wrapper">
        <div class="input-container">
          <textarea
            ref="textareaRef"
            v-model="inputMessage"
            @input="adjustTextareaHeight"
            @focus="handleInputFocus"
            @blur="handleInputBlur"
            @keydown="handleKeyDown"
            placeholder="和豆酱AI聊天，询问电影推荐、年度报告等..."
            class="chat-input"
            :disabled="isLoading"
            rows="1"
          ></textarea>
          <div class="input-actions">
          <button 
            @click="sendMessage" 
            class="send-btn"
            :disabled="isLoading || !inputMessage.trim()"
              :class="{ active: inputMessage.trim() }"
          >
              <span v-if="!isLoading" class="send-icon">🚀</span>
              <span v-else class="loading-icon">⏳</span>
          </button>
        </div>
      </div>
        <div class="input-footer">
          <div class="input-tips">
            <span class="tip">按 Enter 发送，Shift + Enter 换行</span>
          </div>
          <div class="input-counter">
            {{ inputMessage.length }}/2000
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick, watch } from 'vue'
import { useAuthStore } from '@/store/authStore'
import { useRouter } from 'vue-router'
import aiApi from '@/api/ai/aiApi.js'

const authStore = useAuthStore()
const router = useRouter()
const messages = ref([])
const inputMessage = ref('')
const isLoading = ref(false)
const isStreaming = ref(false)
const isInputFocused = ref(false)
const messagesContainer = ref(null)
const textareaRef = ref(null)

// 快速操作按钮
const quickActions = ref([
  { id: 1, icon: '📊', label: '生成年度报告', message: '请为我生成2025年度观影报告' },
  { id: 2, icon: '🎬', label: '推荐电影', message: '请为我推荐一些电影' },
  { id: 3, icon: '🎯', label: '分析偏好', message: '请分析我的观影偏好' },
  { id: 4, icon: '💡', label: '观影建议', message: '请给我一些观影建议' }
])

// 监听消息变化，自动滚动到底部
watch(messages, () => {
  scrollToBottom()
}, { deep: true })

// 组件挂载时发送欢迎消息
onMounted(() => {
  if (authStore.user) {
    addMessage('ai', `你好 **${authStore.user.username}**！👋 我是豆酱AI，你的专属电影助手。

我可以帮你：

<div class="feature-list">
  <div class="feature-item">
    <span class="feature-icon">🎬</span>
    <span class="feature-text">生成年度观影报告</span>
  </div>
  <div class="feature-item">
    <span class="feature-icon">📊</span>
    <span class="feature-text">分析观影偏好</span>
  </div>
  <div class="feature-item">
    <span class="feature-icon">🎯</span>
    <span class="feature-text">推荐个性化电影</span>
  </div>
  <div class="feature-item">
    <span class="feature-icon">💡</span>
    <span class="feature-text">提供观影建议</span>
  </div>
</div>

有什么我可以帮助你的吗？试试下方的快捷按钮吧！✨`)
  } else {
    addMessage('ai', `你好！👋 我是豆酱AI，你的专属电影助手。

<div class="warning-box">
  <span class="warning-icon">⚠️</span>
  <span class="warning-text">请先登录以获取个性化服务</span>
</div>

登录后我可以帮你：

<div class="feature-list">
  <div class="feature-item">
    <span class="feature-icon">🎬</span>
    <span class="feature-text">生成年度观影报告</span>
  </div>
  <div class="feature-item">
    <span class="feature-icon">📊</span>
    <span class="feature-text">分析观影偏好</span>
  </div>
  <div class="feature-item">
    <span class="feature-icon">🎯</span>
    <span class="feature-text">推荐个性化电影</span>
  </div>
  <div class="feature-item">
    <span class="feature-icon">💡</span>
    <span class="feature-text">提供观影建议</span>
  </div>
</div>

你可以先尝试一些通用功能，或者登录后享受完整的个性化服务！🌟`)
  }
})

// 添加消息到聊天记录
function addMessage(type, content) {
  messages.value.push({
    type,
    content,
    timestamp: new Date(),
    liked: false
  })
}

// 滚动到底部
function scrollToBottom() {
  nextTick(() => {
    if (messagesContainer.value) {
      messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
    }
  })
}

// 格式化消息内容（支持丰富的HTML格式）
function formatMessage(content) {
  if (!content) return ''
  
  let formatted = content
  
  // 转换Markdown格式
  formatted = formatted
    // 粗体文本 **text** -> <strong>text</strong>
    .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
    // 斜体文本 *text* -> <em>text</em>
    .replace(/\*(.*?)\*/g, '<em>$1</em>')
    // 代码块 ```code``` -> <pre><code>code</code></pre>
    .replace(/```([\s\S]*?)```/g, '<pre class="code-block"><code>$1</code></pre>')
    // 行内代码 `code` -> <code>code</code>
    .replace(/`(.*?)`/g, '<code class="inline-code">$1</code>')
    // 链接 [text](url) -> <a>text</a>
    .replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2" target="_blank" class="message-link">$1</a>')
    // 换行符
    .replace(/\n/g, '<br>')
    // 分隔线 --- -> <hr>
    .replace(/^---$/gm, '<hr class="divider">')
  
  // 处理列表
  formatted = formatted
    // 无序列表 - item -> <ul><li>item</li></ul>
    .replace(/^- (.+)$/gm, '<li class="list-item">$1</li>')
    .replace(/(<li class="list-item">.*<\/li>)/s, '<ul class="message-list">$1</ul>')
    // 有序列表 1. item -> <ol><li>item</li></ol>
    .replace(/^\d+\. (.+)$/gm, '<li class="numbered-item">$1</li>')
    .replace(/(<li class="numbered-item">.*<\/li>)/s, '<ol class="numbered-list">$1</ol>')
  

  
  return formatted
}

// 格式化时间
function formatTime(timestamp) {
  const now = new Date()
  const messageTime = new Date(timestamp)
  const diffInMinutes = Math.floor((now - messageTime) / (1000 * 60))
  
  if (diffInMinutes < 1) {
    return '刚刚'
  } else if (diffInMinutes < 60) {
    return `${diffInMinutes}分钟前`
  } else if (diffInMinutes < 1440) {
    const hours = Math.floor(diffInMinutes / 60)
    return `${hours}小时前`
  } else {
    return messageTime.toLocaleDateString('zh-CN')
  }
  }
  
// 自动调整输入框高度
function adjustTextareaHeight() {
  nextTick(() => {
    if (textareaRef.value) {
      textareaRef.value.style.height = 'auto'
      const scrollHeight = textareaRef.value.scrollHeight
      const maxHeight = 120 // 最大高度约6行
      textareaRef.value.style.height = Math.min(scrollHeight, maxHeight) + 'px'
    }
  })
}

// 处理键盘事件
function handleKeyDown(event) {
  if (event.key === 'Enter' && !event.shiftKey) {
    event.preventDefault()
    sendMessage()
  }
}

// 输入框焦点事件
function handleInputFocus() {
  isInputFocused.value = true
}

function handleInputBlur() {
  // 延迟设置，避免快速点击按钮时隐藏
  setTimeout(() => {
    isInputFocused.value = false
  }, 100)
}

// 发送消息
async function sendMessage() {
  if (!inputMessage.value.trim() || isLoading.value) return
  
  const message = inputMessage.value.trim()
  addMessage('user', message)
  inputMessage.value = ''
  adjustTextareaHeight()
  
  isLoading.value = true
  
  try {
    console.log('当前用户信息:', authStore.user)
    
    // 如果用户未登录，给出提示
    if (!authStore.user) {
      addMessage('ai', `<div class="warning-box">
        <span class="warning-icon">⚠️</span>
        <span class="warning-text">请先登录以获取个性化服务</span>
      </div>
      
      你可以点击右上角的用户头像进行登录。登录后即可享受完整的AI推荐服务！`)
      isLoading.value = false
      return
    }
    
    const userId = authStore.user.id.toString()
    console.log('准备发送到后端的用户ID:', userId)
    
    // 创建AI消息占位符
    const aiMessageIndex = messages.value.length
    addMessage('ai', '')
    
    // 设置流式输出状态
    isStreaming.value = true
    isLoading.value = true
    
    // 使用流式输出
    await aiApi.chatWithDoujiangStream(
        userId,
      message,
      // onChunk - 处理每个数据块
      (chunk) => {
        console.log('收到数据块:', chunk);
        // 将新内容追加到当前AI消息
        messages.value[aiMessageIndex].content += chunk
        // 强制更新视图
        messages.value = [...messages.value]
      },
      // onComplete - 流式输出完成
      () => {
        console.log('流式输出完成')
        isLoading.value = false
        isStreaming.value = false
      },
      // onError - 错误处理
      (error) => {
        console.error('流式输出错误:', error)
        messages.value[aiMessageIndex].content = `<div class="error-box">
          <span class="error-icon">❌</span>
          <span class="error-text">抱歉，我遇到了一些问题。请稍后再试。</span>
        </div>`
        isLoading.value = false
        isStreaming.value = false
      }
    )
  } catch (error) {
    console.error('发送消息失败:', error)
    addMessage('ai', `<div class="error-box">
      <span class="error-icon">❌</span>
      <span class="error-text">抱歉，我遇到了一些问题。请稍后再试。</span>
    </div>`)
    isLoading.value = false
    isStreaming.value = false
  }
}

// 发送快速消息
function sendQuickMessage(message) {
  inputMessage.value = message
  sendMessage()
}

// 复制消息
function copyMessage(content) {
  // 移除HTML标签
  const plainText = content.replace(/<[^>]*>/g, '')
  navigator.clipboard.writeText(plainText).then(() => {
    // 显示复制成功提示
    console.log('消息已复制到剪贴板')
  })
}

// 点赞消息
function likeMessage(index) {
  messages.value[index].liked = !messages.value[index].liked
}

// 清空对话
function clearChat() {
  if (confirm('确定要清空所有对话记录吗？')) {
    messages.value = []
    // 重新添加欢迎消息
    if (authStore.user) {
      addMessage('ai', `欢迎回来，**${authStore.user.username}**！🎉 对话已清空，让我们开始新的聊天吧！`)
    }
  }
}

// 处理用户头像加载失败
function handleAvatarError(event) {
  // 隐藏加载失败的图片
  event.target.style.display = 'none'
  
  // 显示默认头像容器
  const avatarContainer = event.target.parentElement
  if (avatarContainer) {
    // 移除图片，显示默认头像
    let defaultContainer = avatarContainer.querySelector('.default-avatar-container')
    if (defaultContainer) {
      defaultContainer.style.display = 'flex'
    } else {
      // 如果没有默认头像容器，创建一个
      defaultContainer = document.createElement('div')
      defaultContainer.className = 'default-avatar-container'
      defaultContainer.style.display = 'flex'
      defaultContainer.style.background = getAvatarBackgroundColor()
      defaultContainer.innerHTML = `<span class="default-avatar-text">${getAvatarText()}</span>`
      avatarContainer.appendChild(defaultContainer)
    }
  }
}

// 获取用户头像URL
function getUserAvatarUrl() {
  return authStore.user?.avatar || null;
}

// 获取用户头像文本
function getAvatarText() {
  if (authStore.user?.username) {
    return authStore.user.username.charAt(0).toUpperCase();
  }
  return '?';
}

// 获取用户头像背景色
function getAvatarBackgroundColor() {
  if (!authStore.user?.username) {
    return 'linear-gradient(135deg, #a78bfa 0%, #ec4899 100%)';
  }
  
  // 基于用户名生成固定的颜色
  const colors = [
    'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
    'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
    'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
    'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
    'linear-gradient(135deg, #fa709a 0%, #fee140 100%)',
    'linear-gradient(135deg, #a8edea 0%, #fed6e3 100%)',
    'linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%)',
    'linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%)',
    'linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%)',
    'linear-gradient(135deg, #fad0c4 0%, #ffd1ff 100%)'
  ];
  
  // 使用用户名的字符码总和来选择颜色
  let sum = 0;
  for (let i = 0; i < authStore.user.username.length; i++) {
    sum += authStore.user.username.charCodeAt(i);
  }
  
  return colors[sum % colors.length];
}
</script>

<style scoped>
/* 聊天容器 */
.chat-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  overflow: hidden;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

/* 聊天标题栏 */
.chat-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.ai-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.ai-avatar {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.3) 0%, rgba(255, 255, 255, 0.1) 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
}

.ai-details h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.ai-details p {
  margin: 0;
  font-size: 12px;
  opacity: 0.8;
}

.chat-actions .action-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-actions .action-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

/* 消息区域 */
.chat-messages {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: #f8fafc;
  scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 3px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: #a0aec0;
}

.message-item {
  margin-bottom: 20px;
}

.message {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  max-width: 85%;
}

.message.user {
  flex-direction: row-reverse;
  margin-left: auto;
}

.message-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  padding: 0;
  box-sizing: border-box;
}

.message-avatar:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.message.user .message-avatar {
  color: white;
  border: 2px solid rgba(102, 126, 234, 0.3);
  padding: 2px;
}

.message.user .message-avatar .avatar-container {
  background: transparent;
  /* 确保容器占满整个头像区域 */
  width: 100% !important;
  height: 100% !important;
}

.message.ai .message-avatar {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
  border: 2px solid rgba(240, 147, 251, 0.3);
  padding: 2px;
}

.ai-message-avatar {
  font-size: 18px;
}

.avatar-container {
  width: 100%;
  height: 100%;
  position: relative;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.user-avatar-img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  object-position: center;
  display: block;
  transition: opacity 0.3s ease;
}

.user-avatar-img:loading {
  opacity: 0.5;
}

.user-avatar-img:loaded {
  opacity: 1;
}

.default-avatar-container {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

.default-avatar-text {
  font-size: 16px;
  text-transform: uppercase;
  user-select: none;
}

.message-content {
  flex: 1;
  min-width: 0;
}

.message-bubble {
  background: white;
  border-radius: 18px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  border: 1px solid #f1f5f9;
}

.message.user .message-bubble {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.message-text {
  padding: 16px 20px;
  line-height: 1.6;
  word-wrap: break-word;
  font-size: 14px;
}

.message-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 20px 12px;
  border-top: 1px solid #f1f5f9;
}

.message.user .message-footer {
  border-top: 1px solid rgba(255, 255, 255, 0.2);
}

.message-time {
  font-size: 11px;
  color: #64748b;
}

.message.user .message-time {
  color: rgba(255, 255, 255, 0.8);
}

.message-actions {
  display: flex;
  gap: 4px;
}

.message-actions .action-btn {
  background: transparent;
  border: none;
  color: #64748b;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 12px;
}

.message-actions .action-btn:hover {
  background: #f1f5f9;
  transform: scale(1.1);
}

.message-actions .action-btn.liked {
  color: #3b82f6;
  background: #dbeafe;
}

/* 打字指示器 */
.typing-indicator {
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.typing-dots {
  display: flex;
  gap: 4px;
}

.typing-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #667eea;
  animation: typing 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }

.typing-text {
  font-size: 12px;
  color: #64748b;
  font-style: italic;
}

@keyframes typing {
  0%, 80%, 100% { 
    transform: scale(0.8); 
    opacity: 0.5; 
  }
  40% { 
    transform: scale(1); 
    opacity: 1; 
  }
}

/* 输入区域 */
.chat-input-container {
  background: white;
  border-top: 1px solid #e2e8f0;
  padding: 20px;
}

.quick-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  justify-content: center;
}

.quick-action-btn {
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  border: 1px solid #e2e8f0;
  padding: 10px 16px;
  border-radius: 24px;
  font-size: 13px;
  color: #475569;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.quick-action-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.quick-action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.action-icon {
  font-size: 14px;
}

.action-label {
  font-weight: 500;
}

.input-wrapper {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-container {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  background: #f8fafc;
  border: 2px solid #e2e8f0;
  border-radius: 24px;
  padding: 8px 16px;
  transition: all 0.3s ease;
}

.input-container:focus-within {
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  background: white;
}

.chat-input {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px 0;
  font-size: 14px;
  resize: none;
  outline: none;
  font-family: inherit;
  line-height: 1.5;
  max-height: 120px;
  overflow-y: auto;
}

.chat-input::-webkit-scrollbar {
  width: 4px;
}

.chat-input::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 2px;
}

.chat-input::placeholder {
  color: #94a3b8;
}

.input-actions {
  display: flex;
  align-items: center;
}

.send-btn {
  background: #e2e8f0;
  color: #64748b;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
}

.send-btn.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  transform: scale(1.1);
}

.send-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  transform: scale(1.1);
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.loading-icon {
  animation: rotate 1s linear infinite;
  }
  
@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
  }
  
.input-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  }
  
.input-tips {
  font-size: 11px;
  color: #94a3b8;
  }
  
.input-counter {
  font-size: 11px;
  color: #94a3b8;
}

/* 消息内容样式 */
.message-text :deep(strong) {
  font-weight: 600;
  color: inherit;
}

.message-text :deep(em) {
  font-style: italic;
  color: inherit;
}

.message-text :deep(.code-block) {
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 12px;
  margin: 8px 0;
  overflow-x: auto;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.4;
}

.message.user .message-text :deep(.code-block) {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 255, 0.9);
}

.message-text :deep(.inline-code) {
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  border-radius: 4px;
  padding: 2px 6px;
  font-family: 'Courier New', monospace;
  font-size: 12px;
}

.message.user .message-text :deep(.inline-code) {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.2);
  color: rgba(255, 255, 255, 0.9);
}

.message-text :deep(.message-link) {
  color: #3b82f6;
  text-decoration: none;
  border-bottom: 1px solid #3b82f6;
  transition: all 0.3s ease;
}

.message-text :deep(.message-link:hover) {
  color: #1d4ed8;
  border-bottom-color: #1d4ed8;
}

.message.user .message-text :deep(.message-link) {
  color: rgba(255, 255, 255, 0.9);
  border-bottom-color: rgba(255, 255, 255, 0.9);
}



.message-text :deep(.message-list),
.message-text :deep(.numbered-list) {
  margin: 8px 0;
  padding-left: 0;
}

.message-text :deep(.list-item),
.message-text :deep(.numbered-item) {
  margin: 4px 0;
  padding-left: 20px;
  position: relative;
}

.message-text :deep(.list-item::before) {
  content: '•';
  position: absolute;
  left: 0;
  color: #667eea;
  font-weight: bold;
}

.message-text :deep(.divider) {
  border: none;
  height: 1px;
  background: linear-gradient(to right, transparent, #e2e8f0, transparent);
  margin: 16px 0;
}

.message.user .message-text :deep(.divider) {
  background: linear-gradient(to right, transparent, rgba(255, 255, 255, 0.3), transparent);
}

.message-text :deep(.feature-list) {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 12px 0;
}

.message-text :deep(.feature-item) {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: #f8fafc;
  border-radius: 8px;
  border-left: 3px solid #667eea;
}

.message.user .message-text :deep(.feature-item) {
  background: rgba(255, 255, 255, 0.1);
  border-left-color: rgba(255, 255, 255, 0.5);
}

.message-text :deep(.feature-icon) {
  font-size: 16px;
}

.message-text :deep(.feature-text) {
  font-weight: 500;
}

.message-text :deep(.warning-box),
.message-text :deep(.error-box) {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-radius: 8px;
  margin: 8px 0;
}

.message-text :deep(.warning-box) {
  background: #fef3cd;
  border: 1px solid #fde047;
  color: #92400e;
}

.message-text :deep(.error-box) {
  background: #fecaca;
  border: 1px solid #f87171;
  color: #dc2626;
}

.message-text :deep(.warning-icon),
.message-text :deep(.error-icon) {
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-container {
    height: 100vh;
    border-radius: 0;
  }
  
  .chat-messages {
    padding: 16px;
  }
  
  .message {
    max-width: 95%;
  }
  
  .quick-actions {
    justify-content: center;
    gap: 6px;
  }
  
  .quick-action-btn {
    font-size: 12px;
    padding: 8px 12px;
  }
  
  .chat-input-container {
    padding: 16px;
  }
  
  .input-footer {
    margin-top: 4px;
  }

  .message-text :deep(.feature-item) {
    flex-direction: column;
    text-align: center;
    gap: 6px;
  }
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .chat-container {
    background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  }
  
  .chat-messages {
    background: #0f172a;
  }
  
  .message-bubble {
    background: #1e293b;
    border-color: #334155;
  }
  
  .message-footer {
    border-top-color: #334155;
  }
  
  .input-container {
    background: #1e293b;
    border-color: #334155;
  }
  
  .input-container:focus-within {
    background: #334155;
  }
  
  .chat-input::placeholder {
    color: #64748b;
  }
}
</style> 